#include "StdAfx.h"
//-------------------------------------------------------------------------------------------------
//ProcParamDefString.cpp  - String Parameter Definition for Image Processing Steps       
//-------------------------------------------------------------------------------------------------

#include "TinyXmlLib.h"
#include "TinyXmlHelper.h"
#include "ImageExptLib.h"

#include "ImageProcStepLibAuto.h"

#include "ProcParamDefString.h"
#include "ProcParamString.h"


ProcParamDefString::ProcParamDefString(void) : 
	ProcParamDef(IPluginICProcStepsParamDef::NotFound, IPluginICProcStepsParamDefBadValue, IPluginICProcStepsParamDefBadValue, IPluginICProcStepsParamDef::input),
	_MinLen(IPluginICProcStepsParamDefString::BadLen),
	_MaxLen(IPluginICProcStepsParamDefString::BadLen)
{
}

ProcParamDefString::ProcParamDefString(int index, const char *Name, const char *Description, IPluginICProcStepsParamDef::Direction Dir, int MinLen, int MaxLen) :
	ProcParamDef(index, Name, Description, Dir),
	_MinLen(MinLen),
	_MaxLen(MaxLen)
{
	Validate();
}

ProcParamDefString::~ProcParamDefString(void)
{
}

long MXSTDMETHOD ProcParamDefString::DestroyPtr(void)
{
	long cnt = InterlockedDecrement(&_refCnt);
	if ( cnt == 0 )
		delete this;
	return cnt;
}

long MXSTDMETHOD ProcParamDefString::DuplicatePtr(void)
{
	return InterlockedIncrement(&_refCnt);
}

void *MXSTDMETHOD ProcParamDefString::Dynamic_cast(const char *IId)
{
	void *rc = '\0';

	if (IId != '\0')
	{
		if (strncmp (IPluginICProcStepsParamDefStringIID::GUID(), IId, IMxPluginBaseIID::IIdSize) == 0)
			rc = static_cast<IPluginICProcStepsParamDefString *>(this);
		else if (strncmp (IMxPluginBaseIID::GUID(), IId, IMxPluginBaseIID::IIdSize) == 0)
			rc = static_cast<IMxPluginBase *>(this);
		else
			rc = '\0';

		if (rc != '\0')
			((IMxPluginBase *)rc)->DuplicatePtr();
	}
	return rc;
}

bool ProcParamDefString::IsMatch(IPluginICProcStepsParam  *param, IPluginICProcStepsParamDef::Direction dir)
{
	bool rc = false;

	if ( ProcParamDef::IsMatch(param, dir) == true)		//checks param is not null, param->IsValid, matchingParam->IsValid(), index, name, direction - logs errors
	{
		ProcParamString *paramStr = dynamic_cast<ProcParamString *>(param);
		if (paramStr == '\0')
			ImageExptLib::LogMsg("ProcParamDefString::IsMatch()", "dynamic_cast<ProcParamString *>(%s) is null", (param != '\0') ? param->GetName(): "param is null");
		else
		{
			std::string typeName(paramStr->GetType());
			if (typeName != IPluginICProcStepsParamDefStringP)
				ImageExptLib::LogMsg("ProcParamDefInt::IsMatch()", "%s->GetType() != IPluginICProcStepsParamDefStringP", param->GetName());
			else
			{
				int len = strlen(paramStr->GetValue());
				if ( len < _MinLen)
					ImageExptLib::LogMsg("ProcParamDefString::IsMatch()", "%s->GetValue()->GetLength() is %d which is less than _MinLen %d", param->GetName(), len, _MinLen);						
				else
				{
					if (len > _MaxLen)
						ImageExptLib::LogMsg("ProcParamDefString::IsMatch()", "%s->GetValue()->GetLength() is %d which is more than _MaxLen %d", param->GetName(), len, _MaxLen);	
					else
						rc = true;
				}
			}
		}
	}
	return rc;
}

const char *ProcParamDefString::GetDefaultValueStr()
{
	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamDefString::GetDefaultValueStr", "ERROR: %s param %s is invalid", (ProcParamDef::GetDirection() == IPluginICProcStepsParamDef::input) ? "in" : "out", ProcParamDef::GetName());
	else
	{
		if (_DefaultValue == IPluginICProcStepsParamDefBadDefaultValue)
		{
			char buff[MaxLenDigits+1];
			_DefaultValue ="length min=";
			_DefaultValue += _itoa(_MinLen, buff, 10);
			_DefaultValue += " max=";
			_DefaultValue += _itoa(_MaxLen, buff, 10);
		}
	}
	return _DefaultValue.c_str();
}

const char *ProcParamDefString::GetString()
{
	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamDefString::GetString", "ERROR: %s param %s is invalid", (ProcParamDef::GetDirection() == IPluginICProcStepsParamDef::input) ? "in" : "out", ProcParamDef::GetName());
	else
	{
		if (_Definition == IPluginICProcStepsParamDefInvalidDefinition)
		{
			char buffer[20+1];

			_Definition = (ProcParamDef::GetDirection() == IPluginICProcStepsParamDef::input) ? "[in]  " : "[out] ";
			_Definition += ProcParamDef::GetName();
			_Definition += " (";
			_Definition += GetTypeStr();
			_Definition += ") min length =";

			_Definition += (_itoa_s(_MinLen, buffer, 20, 10) == 0) ? buffer : IPluginICProcStepsParamDefInvalidValue;
			_Definition += " max length=";

			_Definition += (_itoa_s(_MaxLen, buffer, 20, 10) == 0) ? buffer : IPluginICProcStepsParamDefInvalidValue;
			_Definition +=  " - ";
			_Definition += ProcParamDef::GetDescription();
		}
	}
	return _Definition.c_str();
}

bool ProcParamDefString::SetXml(void *xmlParam)
{
	bool rc = false;

	TiXmlElement *param = static_cast<TiXmlElement *>(xmlParam);
	if (param == '\0')
		ImageExptLib::LogMsg("ProcParamDefString::SetXml", "param is null");
	else
	{
		if (ProcParamDef::SetXml(xmlParam) == true)		//base class checks IsValid()
		{
			rc = true;
		}
	}
	return rc;
}

bool	ProcParamDefString::Validate()
{
	bool rc = false;

	if(ProcParamDef::Validate() == true)
	{
		if ((_MinLen < IPluginICProcStepsParamDefString::MinLen) || (_MinLen > IPluginICProcStepsParamDefString::MaxLen) || (_MaxLen < IPluginICProcStepsParamDefString::MinLen) || (_MaxLen > IPluginICProcStepsParamDefString::MaxLen) || (_MinLen > _MaxLen))
		{
			_error = true;
			_MinLen = IPluginICProcStepsParamDefString::BadLen;
			_MaxLen = IPluginICProcStepsParamDefString::BadLen;
		}
		else
			rc = true;
	}
	return rc;
}

//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------